/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import * as fs from 'fs';
import * as path from 'path';

import { FileMetaData } from './file-metadata.js';
import { Hash } from './hash.js';

export interface FileHasher {
  hash(filepath: string, fileMetaData?: FileMetaData): string;
}

export class FileContentHasher implements FileHasher {
  hash(filepath: string): string {
    const buffer = fs.readFileSync(filepath);
    return Hash.hash(buffer);
  }
}

export class FileMetaHasher implements FileHasher {
  hash(filepath: string, fileMetaData: FileMetaData): string {
    return Hash.hash(path.basename(filepath) + fileMetaData.size + fileMetaData.lastModifiedTime);
  }
}
